Why can't we have an immutable version of operator[] for map
Posted
by Yan Cheng CHEOK
on Stack Overflow
See other posts from Stack Overflow
or by Yan Cheng CHEOK
Published on 2010-03-17T03:36:38Z
Indexed on
2010/03/17
3:41 UTC
Read the original article
Hit count: 209
The following code works fine :
std::map<int, int>& m = std::map<int, int>();
int i = m[0];
But not the following code :
// error C2678: binary '[' : no operator...
const std::map<int, int>& m = std::map<int, int>();
int i = m[0];
Most of the time, I prefer to make most of my stuff to become immutable, due to reason :
http://www.javapractices.com/topic/TopicAction.do?Id=29
I look at map source code. It has
mapped_type& operator[](const key_type& _Keyval)
Is there any reason, why std::map unable to provide
const mapped_type& operator[](const key_type& _Keyval) const
© Stack Overflow or respective owner